package com.ltv.xiangqiplugin.models;

import java.util.Vector;

public class Ma extends Piece { // Horse
	private static Ma ma;

	private Ma() {
	}

	public static Vector<Destination> possibleDestinations(char board[][], SquareCoord origin, boolean checkCheck) {
		if (ma == null) {
			ma = new Ma();
		}

		return ma._possibleDestinations(board, origin, checkCheck);
	}

	private Vector<Destination> _possibleDestinations(char board[][], SquareCoord origin, boolean checkCheck) {
		Vector<Destination> moves = new Vector<Destination>();
		char [][] tempBoard = fillTempBoard(board);
		char color = ' ';
		int x = origin.getRow();
		int y = origin.getFile();
		tempBoard[x][y] = ' ';
		char piece = board[x][y];
		if (Character.isUpperCase(board[x][y])) color = 'r';
		else color = 'b';

		// N
		if (x+2 < 10 && board[x+1][y] == ' ') {
			// W
			if (y+1 < 9) {
				checkMove(moves, x+2, y+1, piece, color, tempBoard, board, checkCheck);
			}
			// E
			if (y-1 > -1) {
				checkMove(moves, x+2, y-1, piece, color, tempBoard, board, checkCheck);
			}
		}
		// E
		if (y-2 > -1 && board[x][y-1] == ' ') {
			// N
			if (x+1 < 10) {
				checkMove(moves, x+1, y-2, piece, color, tempBoard, board, checkCheck);
			}
			// S
			if (x-1 > -1) {
				checkMove(moves, x-1, y-2, piece, color, tempBoard, board, checkCheck);
			}
		}
		// S
		if (x-2 > -1 && board[x-1][y] == ' ') {
			// W
			if (y+1 < 9) {
				checkMove(moves, x-2, y+1, piece, color, tempBoard, board, checkCheck);
			}
			// E
			if (y-1 > -1) {
				checkMove(moves, x-2, y-1, piece, color, tempBoard, board, checkCheck);
			}
		}
		// W
		if (y+2 < 9 && board[x][y+1] == ' ') {
			// S
			if (x-1 > -1) {
				checkMove(moves, x-1, y+2, piece, color, tempBoard, board, checkCheck);
			}
			// N
			if (x+1 < 10) {
				checkMove(moves, x+1, y+2, piece, color, tempBoard, board, checkCheck);
			}
		}
		return moves;
	}
}
